﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using LogisticsBLL;
using System.Data;
using Model;
using System.Web.UI.HtmlControls;

namespace WebLosgic.Driver
{
    public partial class DriverXinxi : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlBind();
                BindDriver();
                
            }
        }

        public void BindDriver()
        {
            DriverBLL dr = new DriverBLL();
            int pageIndex = AspNetPagerTool.CurrentPageIndex;
            int pageSize = AspNetPagerTool.PageSize;
            int cout = 0;
            Drivers drs = new Drivers();
            drs.Name = TxtName.Text;
            drs.Sex = rblSex.SelectedValue;
            drs.IDCard= TxtID.Text;

            string Brith = TxtBrity.Text;
            string Brith2 = TxtBrity2.Text;
            string Hirdate = TxtHireDate.Text;
            string hirdate2 = TxtHireDate2.Text;
            if (TxtBrity.Text == "" || TxtBrity2.Text == "" || TxtHireDate.Text == "" || TxtHireDate2.Text == "")
            {
                Brith = DateTime.MinValue.ToString();
                Brith2 = DateTime.MinValue.ToString();
                Hirdate = DateTime.MinValue.ToString();
                hirdate2 = DateTime.MinValue.ToString();
            }

            drs.FK_TruckID = rblBindState.SelectedValue;
            drs.Phone = TxtPhone.Text;
            drs.TeamName = DDLTeamName.SelectedValue;
            drs.Birth =Convert.ToDateTime(Brith);
            drs.Birth = Convert.ToDateTime(Brith2);
            drs.CheckInTime=Convert.ToDateTime(Hirdate);
            drs.CheckdateEnd = Convert.ToDateTime(hirdate2);
            RptBindDrive.DataSource = dr.BindDriver(pageIndex, pageSize, out cout,drs);
            AspNetPagerTool.RecordCount = cout;
            RptBindDrive.DataBind();
        }

      

        public void ddlBind()
        {
            DriverBLL dr = new DriverBLL();
            DataTable ta = dr.ddlbindCarName();
            DDLTeamName.DataSource = ta;
            DDLTeamName.DataTextField = "TeamName";
            DDLTeamName.DataValueField = "TeamName";
            DDLTeamName.DataBind();
            DDLTeamName.Items.Insert(0, new ListItem("-请选择-", "0"));
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            BindDriver();
         
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            TxtName.Text = "";
            TxtPhone.Text = "";
            TxtID.Text = "";
            rblBindState.SelectedValue = "0";
            rblSex.SelectedValue = "-1";
            DDLTeamName.SelectedValue = "0";
            TxtHireDate.Text = "";
            TxtHireDate2.Text = "";
            TxtBrity.Text = "";
            TxtBrity2.Text = "";

        }

        public bool getstate(string state)
        {
            bool str = true;
            if (state != "")
            {
                str = true;
            }
            else if (state == "")
            {
                str = false;
            }

            return str;
        }

        public bool bindlink(string state)
        {
            bool str = true;
            if (state != "")
            {
                str = false;
            }
            else if (state == "")
            {
                str = true;
            }

            return str;
        }


        protected void RptBindDrive_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "delect")
            {
                DriverBLL db = new DriverBLL();
                int id =Convert.ToInt32(e.CommandArgument);
                string sa = db.dropDriver(id);

                string name = Session["names"].ToString();
                string Behavor = "" + name + ": 删除驾驶员";
                int TypeId = 12;
                int UserID = Convert.ToInt32(Session["UserIDs"]);
                string Parameters = @"" + id;
                string ProcName = "delete  Driver where DriverID=@Driverid";
                HttpRequest request = HttpContext.Current.Request;
                string IP = "192.168.01.12";
                int IsExcePion = 0;
                int a = ProcBLL.InserSys(Behavor, TypeId, UserID, Parameters, ProcName, IP, IsExcePion);
                if (a > 0)
                {
                    Page.ClientScript.RegisterStartupScript(GetType(), "", "alert('" + sa + "')", true);
                    BindDriver();
                }
                 
            }
            else if(e.CommandName== "update")
            {
                DriverBLL db = new DriverBLL();
                int id = Convert.ToInt32(e.CommandArgument);
                Response.Redirect("UpDriver.aspx?did="+id);
            }else if(e.CommandName== "UNbound")
            {
                DriverBLL db = new DriverBLL();
                int id = Convert.ToInt32(e.CommandArgument);
                string str = db.lingbind(id);
                string name = Session["names"].ToString();
                string Behavor = "" + name + ": 解除绑定驾驶员";
                int TypeId = 22;
                int UserID = Convert.ToInt32(Session["UserIDs"]);
                string Parameters = @"" + id;
                string ProcName = "delete Contact where FK_TruckID=@fktruckid";
                HttpRequest request = HttpContext.Current.Request;
                string IP = "192.168.01.12";
                int IsExcePion = 0;
                int a = ProcBLL.InserSys(Behavor, TypeId, UserID, Parameters, ProcName, IP, IsExcePion);
                if (a > 0)
                {
                    Page.ClientScript.RegisterStartupScript(GetType(), "", "alert('" + str + "')", true);
                    BindDriver();
                }
                
            }else if(e.CommandName== "BindTruck")
            {
                DriverBLL db = new DriverBLL();
                int id = Convert.ToInt32(e.CommandArgument);
                string names = (e.Item.FindControl("Name") as HtmlTableCell).InnerText;
                Response.Redirect("DriverBindTruck.aspx?did=" + id+"&name="+names);
            }

        }

        protected void AspNetPagerTool_PageChanged(object sender, EventArgs e)
        {
            BindDriver();
        }
    }
}